package com.key.modules.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.key.common.base.RespResult;
import com.key.common.base.TranException;
import com.key.common.constants.Constants;
import com.key.modules.dto.ECConditionDTO;
import com.key.modules.dto.ECustomersDTO;
import com.key.modules.entity.ECustomers;
import com.key.modules.entity.ICustomers;
import com.key.modules.mapper.ECustomersMapper;
import com.key.modules.service.ECustomersService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class ECustomersServiceImpl implements ECustomersService {

    @Autowired
    private ECustomersMapper eCustomersMapper;

    @Autowired
    private RedisTemplate redisTemplate;
    @Override
    public RespResult addECustomers(ECustomersDTO eCustomersDTO) {
        // log.info("{}==添加企业客户", SecurityUtils.getSubject().getPrincipal());
        Long e_no = redisTemplate.opsForValue().increment("e_no");
        ECustomers eCustomers=new ECustomers();
        BeanUtil.copyProperties(eCustomersDTO,eCustomers);
        eCustomers.setENo(String.format("%s%05d", Constants.ECPRE, e_no));
        eCustomers.setCreateTime(new Date());
        int i = eCustomersMapper.insert(eCustomers);
        if (i>0){
            log.info("{}==企业客户添加成功", eCustomers.toString());
            return RespResult.success();
        }
        return RespResult.error("添加失败");
    }

    @Override
    public RespResult findAllECustomers(ECConditionDTO ecConditionDTO) {
        Page<ECustomers> page=new Page<>(ecConditionDTO.getPageNum(),ecConditionDTO.getPageSize());
        LambdaQueryWrapper<ECustomers> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(StrUtil.isNotBlank(ecConditionDTO.getEnterprise()),ECustomers::getEnterprise,ecConditionDTO.getEnterprise())
                .orderBy(ecConditionDTO.getOrder()!=null,ecConditionDTO.getOrder()==Constants.ASC,ECustomers::getCreateTime);
        Page<ECustomers> eCustomersPage = eCustomersMapper.selectPage(page, lambdaQueryWrapper);
        System.out.println("当前页码数："+eCustomersPage.getCurrent());
        System.out.println("每页显示条数："+eCustomersPage.getSize());
        System.out.println("总条数："+eCustomersPage.getTotal());
        System.out.println("总页数："+eCustomersPage.getPages());
        System.out.println("查询数据："+eCustomersPage.getRecords());
        return RespResult.success(eCustomersPage);
    }

    @Override
    public RespResult findECustomersById(Integer id) {
        ECustomers eCustomers = eCustomersMapper.selectById(id);
        if (eCustomers!=null){
            return RespResult.success(eCustomers);
        }
        return RespResult.error("查询失败");
    }

    @Override
    public RespResult updateECustomers(ECustomersDTO eCustomersDTO) {
        // log.info("{}==修改企业客户", SecurityUtils.getSubject().getPrincipal());
        ECustomers eCustomers=new ECustomers();
        BeanUtil.copyProperties(eCustomersDTO,eCustomers);
        int i = eCustomersMapper.updateById(eCustomers);
        if (i>0){
            log.info("{}==修改成功", eCustomers.toString());
            return RespResult.success();
        }
        return RespResult.error("修改失败");
    }

    @Override
    @Transactional
    public RespResult deleteECustomers(List<Integer> ids) {
        // log.info("{}==删除企业客户", SecurityUtils.getSubject().getPrincipal());
        try {
            eCustomersMapper.deleteBatchIds(ids);
            log.info("{}==删除成功",ids);
            return RespResult.success();
        }catch (Exception e){
            throw new TranException("删除失败");
        }
    }

    @Override
    @Transactional
    public RespResult disDSta(List<Integer> ids) {
        try {
            for (Integer id : ids) {
                ECustomers eCustomers=new ECustomers();
                eCustomers.setId(id);
                eCustomers.setEState(Constants.DISABLEDSTA);
                eCustomersMapper.updateById(eCustomers);
            }
            return RespResult.success();
        }catch (Exception e){
            throw new TranException("禁用失败");
        }
    }

    @Override
    @Transactional
    public RespResult enDSta(List<Integer> ids) {
        try {
            for (Integer id : ids) {
                ECustomers eCustomers=new ECustomers();
                eCustomers.setId(id);
                eCustomers.setEState(Constants.ENABLEDSTA);
                eCustomersMapper.updateById(eCustomers);
            }
            return RespResult.success();
        }catch (Exception e){
            throw new TranException("启用失败");
        }
    }
}
